package main

import (
	_ "container/list"
	"fmt"
	"unicode"
)

// grade=3为最高级别
func main() {
	var passwd string
	var grade int
	grade = 0
	fmt.Println("请输入密码：")
	fmt.Scanln(&passwd)
	a := len_passwd(passwd, 0)
	b := sym_passwd(passwd, 0)
	c := rep_passwd(passwd, 0)
	if c == true {
		grade = a + b + 1
	} else {
		grade = a + b
	}
	fmt.Printf("密码等级为：%v", grade)
}

// 长度判断
func len_passwd(passwd string, grade int) int {
	if len(passwd) > 7 {
		grade += 1
	}
	return grade
}

// 要素构成--数字、大写、小写、其它字符
func sym_passwd(passwd string, grade int) int {
	result := make([]int, 4)
	for _, i := range passwd {
		if unicode.IsDigit(i) {
			result[0] = 1
		} else if unicode.IsLower(i) {
			result[1] = 1
		} else if unicode.IsUpper(i) {
			result[2] = 1
		} else {
			result[3] = 1
		}
	}
	sum := 0
	for _, j := range result {
		sum += j
	}
	if sum >= 3 {
		grade += 1
	}
	return grade
}

// 是否有超过3的重复子串
func rep_passwd(passwd string, grade int) bool {
	substrs := make(map[string]bool)
	for i := 0; i <= len(passwd)-4; i++ {
		substr := passwd[i : i+4]
		if _, ok := substrs[substr]; ok {
			return true
		}
	}
	return false
}
